<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style scoped lang="less">
        .form-preview {
            position: relative;
            display: flex;
            align-items: center;
        }

        .form-preview h2 {
            font-size: 18px;
            font-weight: normal;
        }

        .form-preview small {
            font-size: 12px;
            display: inline-block;
            padding: 2px 6px;
            background: #27ba9b;
            color: #fff;
            border-radius: 2px;
            transform: scale(0.8);
        }

        .form-preview .btn-edit {
            font-size: 12px;
            display: none;
            text-decoration: none;
            color: #069;
            margin-left: 20px;
        }

        .form-preview:hover .btn-edit {
            display: block;
        }

        .form-edit {
            padding-top: 20px;
        }

        .form-item {
            display: flex;
            margin-bottom: 20px;
            align-items: center;
        }

        .form-item .label {
            width: 60px;
            text-align: right;
            font-size: 14px;
        }

        .form-item .input {
            flex: 1;
        }

        .form-item input,
        .form-item select {
            appearance: none;
            outline: none;
            border: 1px solid #ccc;
            width: 200px;
            height: 40px;
            box-sizing: border-box;
            padding: 10px;
            color: #666;
        }

        .form-item input::placeholder {
            color: #666;
        }

        .form-item .cancel,
        .form-item .submit {
            appearance: none;
            outline: none;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 4px 10px;
            margin-right: 10px;
            font-size: 12px;
            background: #ccc;
        }

        .form-item .submit {
            border-color: #069;
            background: #069;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="article-case" id="ArticleCase">
        <div v-show="edit" class="form-preview">
            <h2>{{form.title}}</h2>
            <small>{{form.channel}}</small>
            <a @click="bianji" class="btn-edit" href="#">编辑</a>
        </div>
        <div v-show="!edit" class="form-edit">
            <div class="form-item">
                <div class="label">标题：</div>
                <div class="input">
                    <input v-model="article.title" type="text" placeholder="请输入标题" />
                </div>
            </div>
            <div class="form-item">
                <div class="label">频道：</div>
                <div class="input">
                    <select v-model="article.channel">
                        <option value="">请选择频道</option>
                        <option value="前端">前端</option>
                        <option value="运维">运维</option>
                        <option value="测试">测试</option>
                    </select>
                </div>
            </div>
            <div class="form-item">
                <div class="label"></div>
                <div class="input">
                    <button @click="quxiao" class="cancel">取消</button>
                    <button @click="add" class="submit">确认</button>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        new Vue({
            el: '#ArticleCase',
            data() {
                return {
                    edit: false,
                    article: {
                        title: '如何成为一名前端老鸟？',
                        channel: '前端',
                    },
                    form: {
                        title: '如何成为一名前端老鸟？',
                        channel: '前端',
                    },
                };
            },
            methods: {
                add() {
                    if (this.article.channel === '') {
                        alert("你有没有发现你忘了选标签？")
                        return
                    }
                    this.edit = !this.edit
                    this.form.title = this.article.title
                    this.form.channel = this.article.channel
                },
                bianji() {
                    this.edit = !this.edit
                },
                quxiao() {
                    this.edit = !this.edit
                    this.article.title = this.form.title
                    this.article.channel = this.form.channel
                }
            },
        })
        /*
        本题思路补充：
        一个对象用来实时获取值，另一个对象复制另一个对象的值备用；
        1、编辑表单里用双向绑定一个对象，另一个对象做内容呈现
        2、当值被修改时，修改按钮将双向绑定的对象中的内容赋值给另一个对象（内容呈现的对象）
        3、当取消修改时，需要将原来的值，重新复制回双向绑定的对象，实现下一次进入时数据不变
        4、当点编辑时，切换显示隐藏，呈现表单，表单里存放原来的同步的对象的值
        */
    </script>
</body>

</html>